package com.yanqu.road.server.cmd.activity.recallplayer;

import com.yanqu.road.entity.activity.recallplayer.data.RecallPlayerBeBindTaskValue;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.RecallPlayerProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.activity.recallplayer.RecallPlayerMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import io.netty.channel.ChannelHandlerContext;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

@Cmd(code = GameProtocol.S_RECALL_PLAYER_CROSS_BIND, desc = "跨服转发前来绑定的请求")
public class RecallPlayerBindFromCrossCmd implements Command {
    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        RecallPlayerProto.WriteRecallCodeCrossReqServerMsg reqMsg = RecallPlayerProto.WriteRecallCodeCrossReqServerMsg.parseFrom(packet.getBytes());
        RecallPlayerProto.WriteRecallCodeServerReqCrossMsg reqCrossMsg = reqMsg.getReqCrossMsg();
        List<RecallPlayerBeBindTaskValue> valueList = new ArrayList<>();
        for (RecallPlayerProto.BindTaskInfo bindTaskInfo : reqCrossMsg.getTaskInfoList()) {
            RecallPlayerBeBindTaskValue taskValue = new RecallPlayerBeBindTaskValue();
            taskValue.setTaskValue(BigInteger.valueOf(Long.parseLong(bindTaskInfo.getTaskValue())));
            taskValue.setTaskType(bindTaskInfo.getTaskType());
            taskValue.setBindTimeValue(BigInteger.valueOf(Long.parseLong(bindTaskInfo.getTaskBindTimeValue())));
            taskValue.setLastUpdateTime(bindTaskInfo.getUpdateTime());
            if (bindTaskInfo.hasNeedUpdate()) {
                taskValue.setNeedUpdate(bindTaskInfo.getNeedUpdate());
            }
            valueList.add(taskValue);
        }

        int ret = RecallPlayerMgr.recallBind(reqCrossMsg.getReqBindUserId(), reqMsg.getOriUserId(), PlayerBasePb.parseBaseUserInfo(reqCrossMsg.getRecallUserInfo()), reqCrossMsg.getRecallCode(), reqCrossMsg.getReqBindUserType(), valueList);
        RecallPlayerProto.WriteRecallCodeServerRespCrossMsg.Builder respCrossMsg = RecallPlayerProto.WriteRecallCodeServerRespCrossMsg.newBuilder();
        respCrossMsg.setRecallCode(reqCrossMsg.getRecallCode());
        respCrossMsg.setRespServerId(reqMsg.getOriServerId());
        respCrossMsg.setRespUserId(reqMsg.getOriUserId());
        respCrossMsg.setRet(ret);
        YanQuMessage message = YanQuMessageUtils.buildMessage(CrossProtocol.C_PLAYER_RECALL_BIND_RESP, respCrossMsg);
        GamePlayerMgr.sendPacket(0, message);
    }
}
